<?php

require_once('lib/Invite.php');
require_once('lib/Constants.php');
require_once('lib/MixiAppliAPI.php');

$api = new MixiAppliAPI(array_merge($_GET, $_POST));

$api->validate_params(array(
    'member_id' => array('not_blank'),
    'date'      => array('not_blank', 'date'),
    'via'       => array(array('in_array', 'notification', 'message', 'diary')),
));

$member_id_list = explode('-', $api->params['member_id']);

$invite = new Invite();
$invite->owner_id    = $api->viewer_id;
$invite->target_date = $api->params['date'];
$invite->message     = isset($api->params['message']) ? $api->params['message'] : '';
$invite->via         = isset($api->params['via']) ? $api->params['via'] : '';

if (!$invite->create())
    $api->set_error('could not create invite');

foreach ($member_id_list as $member_id) {
    if (!is_numeric($member_id)) continue;
    if (!$invite->add($member_id)) {
        $invite->delete();
        $api->set_error('could not add member member_id: '.$member_id);
    }
}

$api->set_result($invite);

?>
